<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/2/20
 * Time: 16:46
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Member extends Base
{
    /**
     * 用户列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $where['user_status'] = ['neq', 3];
            if(isset($keyword) && $keyword != ''){
                $where['user_username|user_d_name|user_d_company|user_d_zhiwu_name|user_remark|user_phone'] = ['like', '%' . $keyword . '%'];
            }
            if(isset($start) && $start != "" && isset($end) && $end == "")
            {
                $where['user_time'] = ['>=',$start];
            }
            if(isset($end) && $end != "" && isset($start) && $start == "")
            {
                $where['user_time'] = ['<=',$end];
            }
            if(isset($start) && $start != "" && isset($end) && $end != "")
            {
                $where['user_time'] = ['between',[$start,$end]];
            }
            if(isset($user_d_type) && $user_d_type != "")
            {
                $where['user_d_type'] = $user_d_type;
            }
            if(isset($user_status) && $user_status != "")
            {
                $where['user_status'] = $user_status;
            }
            if(isset($user_type) && $user_type != "")
            {
                $where['user_type'] = ['like','%'.$user_type.'%'];
            }
            if(isset($user_my) && $user_my != "")
            {
                $where['user_my'] = $user_my;
            }
            if(isset($indexindex) && $indexindex != "")
            {
                $where['user_d_index'] = $indexindex;
            }
            if(isset($user_d_zhiwu) && $user_d_zhiwu != "")
            {
                $where['user_d_zhiwu'] = $user_d_zhiwu;
            }
            if(isset($room_id) && $room_id){ // 空间
                $where['user_d_room'] = ['like','%'.$room_id.'%'];
            }
            if(isset($style_id) && $style_id){ // 风格
                $where['user_d_style'] = ['like','%,' . $style_id . ',%'];
            }
            $field=input('field');//字段
            $order=input('order');//排序方式
            if($field && $order){
                $od = $field." ".$order;
            }else{
                $od = "user_status asc,user_time desc";
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('u_user')->alias('c')
                ->join('zhiwu z','c.user_d_zhiwu=z.zhiwu_id','LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('u_user')->alias('c')
                ->join('zhiwu z','c.user_d_zhiwu=z.zhiwu_id','LEFT')
                ->where($where)
                ->page($NowPage,$limits)
                ->order($od)
                ->field('c.*,zhiwu_name')
                ->select();
            if($lists){
                foreach ($lists as $k => $v){
                    $userstr = '';
//                    $lists[$k]['auths'] = '不限';
//                    if($v['user_auth']){
//                        $auth = explode(',',trim($v['user_auth'],','));
//                        $names = Db::name('ten')
//                            ->where('ten_id','in',$auth)
//                            ->column('ten_name');
//                        if(in_array(11,$auth)) $names[] = '品牌优惠';
//                        if(in_array(12,$auth)) $names[] = '人才招聘';
//                        $names = implode($names,',');
//                        $lists[$k]['auths'] = $names;
//                    }
                    //-----------------
                    $lists[$k]['roomnames'] = '';
                    if($v['user_d_type'] == 2 && trim($v['user_d_room'],',')){
                        $auth = trim($v['user_d_room'],',');
                        $names = Db::name('room_copy')
                            ->where('room_id','in',$auth)
                            ->column('room_name');
                        $lists[$k]['roomnames'] = implode($names,',');
                    }
                    //-----------------
                    ////-----------------
                    $lists[$k]['stylenames'] = '';
                    if($v['user_d_type'] == 2 && trim($v['user_d_style'],',')){
                        $auth = trim($v['user_d_style'],',');
                        $names = Db::name('style')
                            ->where('style_id','in',$auth)
                            ->column('style_name');
                        $lists[$k]['stylenames'] = implode($names,',');
                    }
                    //-----------------
                    $lists[$k]['shejifei'] = $v['user_d_money_min'] . '-' . $v['user_d_money_max'];

                    $auth2 = explode(',',$v['user_type']);
                    if(in_array(1,$auth2)) $userstr .= '普通用户,';
                    if(in_array(2,$auth2)) $userstr .= '合作商,';
                    if(in_array(3,$auth2)) $userstr .= '业务人员,';
                    if(in_array(4,$auth2)) $userstr .= '内部人员,';
                    $userstr = trim($userstr,',');
                    $lists[$k]['user_type_name'] = $userstr;
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $res = $this->getoptions();
        $this->assign('fenlei',$res[0]);
        $this->assign('zhiwu',$res[1]);
        $this->assign('qus',$res[2]);
        $this->assign('styles',$res[3]);
        return $this->fetch();
    }
    
    /**
     * 冻结
     * @return \think\response\Json
     */
    public function down(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
     /**
     * 冻结
     * @return \think\response\Json
     */
    public function delu(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id', $user_id)->update([
            'user_status' => 3 ,
            'user_token' => $user_id ,
            'user_phone' => $user_id ,
        ]);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    /**
     * 推荐
     * @return \think\response\Json
     */
    public function indexshow(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_d_index', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    /**
     * 取消推荐
     * @return \think\response\Json
     */
    public function indexhide(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_d_index', 1);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    /**
     * 单元格编辑
     * @return mixed|\think\response\Json
     */
    public function updateSort(){
        $id = input('id');
        $field = input('field');
        $value = input('value');
        $param = [
            $field => $value,
        ];
        if($field == 'user_phone'){
            if(!preg_match("/^1[123456789]\d{9}$/", $value)){
                return json(['code'=>100,'msg'=>'请填写正确的手机号码']);
            }
            $have = Db::name('u_user')
                ->where('user_phone',$value)
                ->where('user_id','<>',$id)
                ->find();
            if($have) return json(['code' => 100, 'msg' => '此号码已存在！']);
        }
        $res = Db::name('u_user')->where('user_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    //-------------------------------------------------------------------------------------------------------
    //-------------------------------------------------------------------------------------------------------
    /**
     * 批量设置优惠信息
     * @return mixed
     */
    public function excelAll1(){
        if(Request::instance()->isPost()){
            extract(input());
            $today = date('Y-m-d H:i:s');
            $res = Db::table('up_zzz')
                ->where('user_id',1)
                ->update([
                    'user_d_youhui' => $user_d_youhui,
                    'user_d_youhui2' => $user_d_youhui2,
                    'user_d_youhui3' => $user_d_youhui3,
                    'user_d_youhui4' => $user_d_youhui4,
                    'user_updatetime' => $today
                ]);
            $res = Db::table('up_u_user')
                ->where('user_id','in',$user_id)
                ->update([
                    'user_d_youhui' => $user_d_youhui,
                    'user_d_youhui2' => $user_d_youhui2,
                    'user_d_youhui3' => $user_d_youhui3,
                    'user_d_youhui4' => $user_d_youhui4,
                    'user_updatetime' => $today
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $user_id = input('user_id');
        $info = Db::name('zzz')
            ->where('user_id',1)
            ->find();
        $this->assign([
            'info' => $info,
            'user_id' => $user_id
        ]);
        return $this->fetch('member/set1');
    }
    /**
     * 批量设置佣金信息
     * @return mixed
     */
    public function excelAll2(){
        if(Request::instance()->isPost()){
            extract(input());
            $today = date('Y-m-d H:i:s');
            $res = Db::table('up_zzz')
                ->where('user_id',1)
                ->update([
                    'user_d_yongjin' => $user_d_yongjin,
                    'user_d_yongjin2' => $user_d_yongjin2,
                    'user_d_yongjin3' => $user_d_yongjin3,
                    'user_d_yongjin4' => $user_d_yongjin4,
                    'user_d_yongjin5' => $user_d_yongjin5,
                    'user_updatetime' => $today
                ]);
            $res = Db::table('up_u_user')
                ->where('user_id','in',$user_id)
                ->update([
                    'user_d_yongjin' => $user_d_yongjin,
                    'user_d_yongjin2' => $user_d_yongjin2,
                    'user_d_yongjin3' => $user_d_yongjin3,
                    'user_d_yongjin4' => $user_d_yongjin4,
                    'user_d_yongjin5' => $user_d_yongjin5,
                    'user_updatetime' => $today
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $user_id = input('user_id');
        $info = Db::name('zzz')
            ->where('user_id',1)
            ->find();
        $this->assign([
            'info' => $info,
            'user_id' => $user_id
        ]);
        return $this->fetch('member/set2');
    }
    /**
     * 批量设置优惠图片
     * @return mixed
     */
    public function excelAll3(){
        if(Request::instance()->isPost()){
            extract(input());
            $today = date('Y-m-d H:i:s');
            if($type_img){
                $res = Db::table('up_user_banner')
                    ->where('pb_where',2)
                    ->where('pb_pp_id','in',$user_id)
                    ->update([
                        'pb_url' => $type_img,
                        'pb_time' => $today
                    ]);
            }else{
                $res = Db::table('up_user_banner')
                    ->where('pb_where',2)
                    ->where('pb_pp_id','in',$user_id)
                    ->delete();
            }
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $user_id = input('user_id');
        $info = Db::name('zzz')
            ->where('user_id',1)
            ->find();
        $this->assign([
            'info' => $info,
            'user_id' => $user_id
        ]);
        return $this->fetch('member/set3');
    }
    /**
     * 批量设置佣金图片
     * @return mixed
     */
    public function excelAll4(){
        if(Request::instance()->isPost()){
            extract(input());
            $today = date('Y-m-d H:i:s');
            if($type_img){
                $res = Db::table('up_user_banner')
                    ->where('pb_where',3)
                    ->where('pb_pp_id','in',$user_id)
                    ->update([
                        'pb_url' => $type_img,
                        'pb_time' => $today
                    ]);
            }else{
                $res = Db::table('up_user_banner')
                    ->where('pb_where',3)
                    ->where('pb_pp_id','in',$user_id)
                    ->delete();
            }
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $user_id = input('user_id');
        $info = Db::name('zzz')
            ->where('user_id',1)
            ->find();
        $this->assign([
            'info' => $info,
            'user_id' => $user_id
        ]);
        return $this->fetch('member/set4');
    }
    //-------------------------------------------------------------------------------------------------------
    //-------------------------------------------------------------------------------------------------------

    /**
     * 启用
     * @return \think\response\Json
     */
    public function up(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    /**
     * 新增
     * @return mixed
     */
    public function newAdd(){
        if(Request::instance()->isPost()){
            extract(input());
            if(!preg_match("/^1[123456789]\d{9}$/", $user_phone)){
                return json(['code'=>100,'msg'=>'请填写正确的手机号码']);
            }
            $today = date('Y-m-d H:i:s');
            $insert = [
                'user_username' => $user_username,
                'user_remark' => $user_remark,
                'user_image' => $user_image,
                'user_phone' => $user_phone,
                'user_d_type' => $user_d_type,
                'user_time' => $today,
                'user_updatetime' => $today,
            ];
            $about_content1 = Db::table('up_about')->where('id',19)->value('content');
            $about_content2 = Db::table('up_about')->where('id',20)->value('content');
            if($about_content1 == 1) $insert['user_d_auth_uids'] = '';
            if($about_content1 == 2) $insert['user_d_auth_uids'] = -1;
            if($about_content2 == 1) $insert['user_d_auth_sids'] = '';
            if($about_content2 == 2) $insert['user_d_auth_sids'] = -1;
//            if(isset($tens) && $tens){
//                $insert['user_auth'] = implode($tens,',');
//            }
            $have = Db::table('up_u_user')->where('user_phone',$user_phone)->find();
            if($have){
                return json(['code' => 100, 'msg' => '已经有此手机号了！']);
            }
            $res = Db::table('up_u_user')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $tens = Db::name('ten')
            ->field('ten_id,ten_name')
            ->order('ten_status asc,ten_sort desc')
            ->select();
        $this->assign('tens',$tens);
        $this->assign('type',1);
        return $this->fetch('member/useradd');
    }
    /**
     * 新增设计师
     * @return mixed
     */
    public function newAdd2(){
        if(Request::instance()->isPost()){
            extract(input());
            if($user_phone && !preg_match("/^1[123456789]\d{9}$/", $user_phone)){
                return json(['code'=>100,'msg'=>'请填写正确的手机号码']);
            }
            $today = date('Y-m-d H:i:s');
            $insert = [
                'user_username' => $user_username,
                'user_remark' => $user_remark,
                'user_image' => $user_image,
                'user_phone' => $user_phone,
                'user_d_type' => 2,
                'user_d_name' => $user_d_name,
                'user_d_sex' => $user_d_sex,
                'user_d_zhiwu' => $user_d_zhiwu,
                'user_d_zhiwu_name' => $user_d_zhiwu_name,
                'user_d_room' => $user_d_room,
                'user_d_qu' => $user_d_qu,
                'user_d_money_min' => $user_d_money_min,
                'user_d_money_max' => $user_d_money_max,
                'user_d_style' => ',' . $user_d_style . ',',
                'user_d_two_pic' => $user_d_two_pic,
                'user_d_star' => $user_d_star,
                'user_d_company' => $user_d_company,
                'user_d_company_address' => $user_d_company_address,
                'user_d_youhui' => $user_d_youhui,
                'user_d_youhui2' => $user_d_youhui2,
                'user_d_youhui3' => $user_d_youhui3,
                'user_d_youhui4' => $user_d_youhui4,
                'user_d_yongjin' => $user_d_yongjin,
                'user_d_yongjin2' => $user_d_yongjin2,
                'user_d_yongjin3' => $user_d_yongjin3,
                'user_d_yongjin4' => $user_d_yongjin4,
                'user_d_yongjin5' => $user_d_yongjin5,
                'user_d_year' => $user_d_year,
                'user_d_jianjie' => $user_d_jianjie,
                'user_sort' => $user_sort,
                'user_time' => $today,
                'user_updatetime' => $today,
            ];
            $lonlat = explode(',',$lonlat);
            $insert['user_lat'] = $lonlat[0];
            $insert['user_lon'] = $lonlat[1];
            $letter = $this->userLetter(trim($user_d_name,' '));
            $letter2 = $this->userLetter(trim($user_d_company,' '));
            $insert['user_letter'] = $letter;
            $insert['user_letter_com'] = $letter2;
            $have = Db::table('up_u_user')->where('user_phone',$user_phone)->find();
            if($have){
                return json(['code' => 100, 'msg' => '已经有此手机号了！']);
            }
            $about_content1 = Db::table('up_about')->where('id',19)->value('content');
            $about_content2 = Db::table('up_about')->where('id',20)->value('content');
            if($about_content1 == 1) $insert['user_d_auth_uids'] = '';
            if($about_content1 == 2) $insert['user_d_auth_uids'] = -1;
            if($about_content2 == 1) $insert['user_d_auth_sids'] = '';
            if($about_content2 == 2) $insert['user_d_auth_sids'] = -1;
            $res = Db::table('up_u_user')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $res = $this->getoptions();
        $this->assign('fenlei',$res[0]);
        $this->assign('zhiwu',$res[1]);
        $this->assign('qus',$res[2]);
        $this->assign('styles',$res[3]);
        return $this->fetch('member/useradd2');
    }
    /**
     * 编辑
     * @return mixed
     */
    public function memberDetail(){
        if(Request::instance()->isPost()){
            extract(input());
            if(!preg_match("/^1[123456789]\d{9}$/", $user_phone)){
                return json(['code'=>100,'msg'=>'请填写正确的手机号码']);
            }
            $today = date('Y-m-d H:i:s');
            $update = [
                'user_username' => $user_username,
                'user_remark' => $user_remark,
                'user_image' => $user_image,
                'user_phone' => $user_phone,
                'user_updatetime' => $today,
            ];
            if(isset($tens) && $tens){
                $update['user_auth'] = implode($tens,',');
            }else{
                $update['user_auth'] = '';
            }
            $have = Db::table('up_u_user')
                ->where('user_id','<>',$user_id)
                ->where('user_phone',$user_phone)
                ->find();
            if($have){
                return json(['code' => 100, 'msg' => '已经有此手机号了！']);
            }
            $res = Db::table('up_u_user')->where('user_id', $user_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $user_id = input('user_id');
        $info = Db::table('up_u_user')
            ->where('user_id',$user_id)
            ->find();
        $open = explode(',',trim($info['user_auth'],','));
        $tens = Db::name('ten')
            ->field('ten_id,ten_name')
            ->order('ten_status asc,ten_sort desc')
            ->select();
        $this->assign('tens',$tens);
        $this->assign('open',$open);
        $this->assign('info', $info);
        return $this->fetch('member/memberdetail');
    }
    /**
     * 编辑设计师
     * @return mixed
     */
    public function memberDetail2(){
        if(Request::instance()->isPost()){
            extract(input());
            if($user_phone && !preg_match("/^1[123456789]\d{9}$/", $user_phone)){
                return json(['code'=>100,'msg'=>'请填写正确的手机号码']);
            }
            $today = date('Y-m-d H:i:s');
            $update = [
                'user_username' => $user_username,
                'user_remark' => $user_remark,
                'user_image' => $user_image,
                'user_phone' => $user_phone,
                'user_d_name' => $user_d_name,
                'user_d_sex' => $user_d_sex,
                'user_d_zhiwu' => $user_d_zhiwu,
                'user_d_zhiwu_name' => $user_d_zhiwu_name,
                'user_d_room' => $user_d_room,
                'user_d_qu' => $user_d_qu,
                'user_d_money_min' => $user_d_money_min,
                'user_d_money_max' => $user_d_money_max,
                'user_d_style' => ',' . $user_d_style . ',',
                'user_d_two_pic' => $user_d_two_pic,
                'user_d_star' => $user_d_star,
                'user_d_company' => $user_d_company,
                'user_d_company_address' => $user_d_company_address,
                'user_d_youhui' => $user_d_youhui,
                'user_d_youhui2' => $user_d_youhui2,
                'user_d_youhui3' => $user_d_youhui3,
                'user_d_youhui4' => $user_d_youhui4,
                'user_d_yongjin' => $user_d_yongjin,
                'user_d_yongjin2' => $user_d_yongjin2,
                'user_d_yongjin3' => $user_d_yongjin3,
                'user_d_yongjin4' => $user_d_yongjin4,
                'user_d_yongjin5' => $user_d_yongjin5,
                'user_d_year' => $user_d_year,
                'user_d_jianjie' => $user_d_jianjie,
                'user_sort' => $user_sort,
                'user_updatetime' => $today,
            ];
            $have = Db::table('up_u_user')
                ->where('user_id','<>',$user_id)
                ->where('user_phone',$user_phone)
                ->find();
            if($have){
                return json(['code' => 100, 'msg' => '已经有此手机号了！']);
            }
            $lonlat = explode(',',$lonlat);
            $update['user_lat'] = $lonlat[0];
            $update['user_lon'] = $lonlat[1];
            $letter = $this->userLetter(trim($user_d_name,' '));
            $letter2 = $this->userLetter(trim($user_d_company,' '));
            $update['user_letter'] = $letter;
            $update['user_letter_com'] = $letter2;
            $res = Db::table('up_u_user')->where('user_id', $user_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $user_id = input('user_id');
        $info = Db::table('up_u_user')
            ->where('user_id',$user_id)
            ->find();
        $info['user_d_room'] = explode(',',$info['user_d_room']);
        $info['user_d_style'] = explode(',',trim($info['user_d_style'],','));
        $this->assign('info', $info);
        $res = $this->getoptions();
        $this->assign('fenlei',$res[0]);
        $this->assign('zhiwu',$res[1]);
        $this->assign('qus',$res[2]);
        $this->assign('styles',$res[3]);
        return $this->fetch('member/memberdetail2');
    }
    /**
     * 案例相关权限设置
     * @return mixed
     */
    public function authCase(){
        if(Request::instance()->isPost()){
            extract(input());
            if(isset($user_type) && $user_type){
                $user_type = implode($user_type,',');
            }else{
                return json(['code' => 100, 'msg' => '用户权限不能为空']);
            }
            $today = date('Y-m-d H:i:s');
            $update = [
                'user_d_type' => $user_d_type,
                'user_type' => $user_type,
//                'user_my' => $user_my,
//                'user_phone_auth' => $user_phone_auth,
                'user_d_phone_auth' => $user_d_phone_auth,
                'user_yongjin_auth' => $user_yongjin_auth,
                'user_youhui_auth' => $user_youhui_auth,
                'user_d_yongjin_auth' => $user_d_yongjin_auth,
                'user_d_youhui_auth' => $user_d_youhui_auth,
                'user_users' => $user_users,
                'user_cases' => $user_cases,
                'user_sps' => $user_sps,
                'user_shangchuan' => $user_shangchuan,
                'user_updatetime' => $today,
            ];
            if($user_see_cases_type == 1) $update['user_see_cases'] = '';
            if($user_see_cases_type == 2) $update['user_see_cases'] = -1;
            if($user_see_cases_type == 3) {
                if(!$user_see_cases) return json(['code' => 100, 'msg' => '请选择案例']);
                $update['user_see_cases'] = $user_see_cases;
            }
            if($user_d_auth_uids_type == 1) $update['user_d_auth_uids'] = '';
            if($user_d_auth_uids_type == 2) $update['user_d_auth_uids'] = -1;
            if($user_d_auth_uids_type == 3) {
                if(!$user_d_auth_uids) return json(['code' => 100, 'msg' => '请选择设计师']);
                $update['user_d_auth_uids'] = $user_d_auth_uids;
            }
            if($user_d_auth_sids_type == 1) $update['user_d_auth_sids'] = '';
            if($user_d_auth_sids_type == 2) $update['user_d_auth_sids'] = -1;
            if($user_d_auth_sids_type == 3) {
                if(!$user_d_auth_sids) return json(['code' => 100, 'msg' => '请选择门店']);
                $update['user_d_auth_sids'] = $user_d_auth_sids;
            }
            $res = Db::table('up_u_user')->where('user_id', $user_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $user_id = input('user_id');
        $info = Db::table('up_u_user')
            ->where('user_id',$user_id)
            ->find();
        if(!$info['user_d_auth_uids']) {
            $sel = 1;
        }else if($info['user_d_auth_uids'] == -1) {
            $sel = 2;
        }else{
            $sel = 3;
        }
        if(!$info['user_d_auth_sids']) {
            $sel2 = 1;
        }else if($info['user_d_auth_sids'] == -1) {
            $sel2 = 2;
        }else{
            $sel2 = 3;
        }
        if(!$info['user_see_cases']) {
            $sel3 = 1;
        }else if($info['user_see_cases'] == -1) {
            $sel3 = 2;
        }else{
            $sel3 = 3;
        }
        $info['user_type'] = explode(',',$info['user_type']);
        $info['user_d_auth_uids'] = explode(',',$info['user_d_auth_uids']);
        $info['user_d_auth_sids'] = explode(',',$info['user_d_auth_sids']);
        $info['user_cases'] = explode(',',$info['user_cases']);
        $info['user_sps'] = explode(',',$info['user_sps']);
        $info['user_users'] = explode(',',$info['user_users']);
        $info['user_see_cases'] = explode(',',$info['user_see_cases']);
        $this->assign('info', $info);
        $users2 = Db::table('up_u_user')
            ->where('user_status',1)
            ->where('user_d_type',2)
            ->order('user_letter asc')
            ->field('user_id,user_d_name,user_phone')
            ->select();
        $this->assign('users2',$users2);
        $this->assign('sel',$sel);
        $this->assign('sel2',$sel2);
        $this->assign('sel3',$sel3);
        //
        $cases = Db::table('up_case')
            ->where('case_status',1)
            ->field('case_id,case_name,case_address')
            ->order('case_letter asc')
            ->select();
        $this->assign('cases',$cases);
        $province = Db::table('up_types')
            ->field('t_id value,t_name name')
            ->where('t_status',1)
//            ->where('t_id','<',3)
            ->select();
        $province_s = $province;
        foreach ($province as $k => $v){
            $sons = Db::name('pp')
                ->where('pp_t_id',$v['value'])
                ->where('pp_status',1)
                ->field('pp_id value,pp_name name')
                ->select();
            $sons_s = $sons;
            foreach ($sons as $k2 => $v2){
                $sons2 = Db::name('store')
                    ->where('s_pp_id',$v2['value'])
                    ->where('s_status',1)
                    ->field('s_id value,s_name name')
                    ->select();
                $sons2_s = $sons2;
                if($info['user_sps']){
                    foreach ($sons2 as $k3 => $v3){
                        if(in_array($v3['value'],$info['user_sps'])) $sons2[$k3]['selected'] = true;

                        if(in_array($v3['value'],$info['user_d_auth_sids'])) $sons2_s[$k3]['selected'] = true;
                    }
                }
                $sons[$k2]['value'] = 'p' . $v2['value'];
                $sons[$k2]['children'] = $sons2;

                $sons_s[$k2]['value'] = 'p' . $v2['value'];
                $sons_s[$k2]['children'] = $sons2_s;

            }
            $province[$k]['value'] = 't' . $v['value'];
            $province[$k]['children'] = $sons;

            $province_s[$k]['value'] = 't' . $v['value'];
            $province_s[$k]['children'] = $sons_s;
        }
        $this->assign('province',json_encode($province));
        $this->assign('province_s',json_encode($province_s));
        return $this->fetch('member/case');
    }
    /**
     * 用户账单
     * @return mixed|\think\response\Json
     */
    public function userdate(){
        if(request()->isAjax()){
            extract(input());
            $where = [];
            if(isset($keyword) && $keyword != ''){
                $where['user_username|user_phone'] = ['like', '%' . $keyword . '%'];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $field = 'ud.*,user_username,user_phone';
            $count = Db::name('user_date ud')
                ->join('u_user u','ud.ud_user_id=u.user_id','left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('user_date ud')
                ->join('u_user u','ud.ud_user_id=u.user_id','left')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('ud_update desc')
                ->field($field)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    public function getoptions()
    {
        $qus = Db::table('up_area')
            ->where('pid',108)
            ->field('district_id id,district name')
            ->select();
        $zhiwu = Db::table('up_zhiwu')
            ->field('zhiwu_id id,zhiwu_name name')
            ->order('zhiwu_sort desc')
            ->select();
        $fenlei = Db::table('up_room_copy')
            ->field('room_id id,room_name name')
            ->order('room_sort desc')
            ->select();
        $style = Db::table('up_style')
            ->field('style_id id,style_name name')
            ->order('style_sort desc')
            ->select();
        $res = [$fenlei,$zhiwu,$qus,$style];
        return $res;
    }
    //-------------------------------------------------轮播图------------------------------------------------
    /**
     * 轮播图首页展示
     * @return mixed|\think\response\Json
     */
    public function newBanner(){
        if(request()->isAjax ()){
            extract(input());
            $where = [
                'pb_pp_id' => session('design_id')
            ];
            if(isset($keyword) && $keyword != ''){
                $where['pb_title|pb_title_up'] = ['like', '%' . $keyword . '%'];
            }
            if(isset($pb_where) && $pb_where != "")
            {
                $where['pb_where'] = $pb_where;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('user_banner')->where($where)->count();// 获取总条数
            $lists = Db::name('user_banner')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('pb_where asc,pb_sort desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $user_id = input('user_id');
        session('design_id',$user_id);
        return $this->fetch('member/banner');
    }
    /**
     * 添加
     * @return mixed|\think\response\Json
     */
    public function bannerAdd(){
        if(Request::instance()->isPost()){
            extract(input());
            $cpd_content1 = trim($cpd_content1,', ');
            if(!$cpd_content1){
                return json(['code' => 100, 'msg' => '上传内容不能为空！']);
            }
            $cpd_content1 = explode(',',$cpd_content1);
            $data = [];
            foreach ($cpd_content1 as $k => $v){
                $mww = [
                    'pb_title' => $pb_title,
                    'pb_title_up' => $pb_title_up,
                    'pb_pp_id' => session('design_id'),
                    'pb_type' => 1,
                    'pb_sort' => $pb_sort,
                    'pb_where' => $pb_where,
                    'pb_url' => $v,
                    'pb_time' => date('Y-m-d H:i:s')
                ];
                $data[] = $mww;
            }
            $res = Db::table('up_user_banner')->insertAll($data);
            if($res){
                $user_d_zhaopin = Db::table('up_u_user')->where('user_id',session('design_id'))->value('user_d_zhaopin');
                if($pb_where == 5 && $user_d_zhaopin == 1) Db::table('up_u_user')->where('user_id',session('design_id'))->setField('user_d_zhaopin',2);
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $type = input('type');
        $this->assign('type',$type);
        return $this->fetch('member/banneradd');
    }
    /**
     * 编辑
     * @return mixed
     */
    public function bannerEdit(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'pb_title' => $pb_title,
                'pb_title_up' => $pb_title_up,
                'pb_where' => $pb_where,
                'pb_url' => $pb_url,
                'pb_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_user_banner')->where('pb_id',$pb_id)->update($data);
            if($res){
                $user_d_zhaopin = Db::table('up_u_user')->where('user_id',session('design_id'))->value('user_d_zhaopin');
                if($pb_where == 5 && $user_d_zhaopin == 1) Db::table('up_u_user')->where('user_id',session('design_id'))->setField('user_d_zhaopin',2);
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $pb_id = input('pb_id');
        $info = Db::name('user_banner')
            ->where('pb_id',$pb_id)
            ->find();
        $this->assign([
            'banner' => $info,
        ]);
        return $this->fetch('member/banneredit');
    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function bannerDel(){
        $nt_id = input('id');
        $res = Db::table('up_user_banner')->where('pb_id',$nt_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     * 批量删除
     * @return \think\response\Json
     */
    public function batchDel(){
        $nt_id = input('ids');
        $res = Db::table('up_user_banner')->where('pb_id','in',$nt_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     * 设置图片
     * @return \think\response\Json
     */
    public function batchSetTwoPics(){
        $nt_id = input('ids');
        if(!$nt_id) return json(['code' => 100, 'msg' => '请选择数据！']);
        $nt_id = explode(',',trim($nt_id,','));
        if(count($nt_id) > 2) return json(['code' => 100, 'msg' => '最多只能选择两条！']);

        $have = Db::table('up_user_banner')
            ->where('pb_id','in',$nt_id)
            ->where('pb_type','in','2,3')
            ->find();
        if($have) return json(['code' => 100, 'msg' => '只能选择图片类型的！']);
        $data = Db::table('up_user_banner')
            ->where('pb_id','in',$nt_id)
            ->find();
        $pics = Db::table('up_user_banner')
            ->where('pb_id','in',$nt_id)
            ->column('pb_url');
        $pics = implode($pics,',');
        Db::table('up_u_user')
            ->where('user_id',$data['pb_pp_id'])
            ->setField('user_d_two_pic',$pics);
        return json(['code' => 200, 'msg' => '启用成功']);
    }
    /**
     * 排序编辑
     * @return mixed|\think\response\Json
     */
    public function updateBannerSort(){
        $id = input('id');
        $param = [
            'pb_sort'=>input('value'),
        ];
        $res = Db::name('user_banner')->where('pb_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    /**
     * 批量批量
     * @return mixed|\think\response\Json
     */
    public function ppll(){
        $id = input('id');
        $field = input('field');
        $param = [
            $field=>input('value'),
        ];
        $res = Db::table('up_u_user')->where('user_id','in',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    /**
     * 推荐
     * @return \think\response\Json
     */
    public function changeMainStatus(){
        if(session('agid') == 10){
            return json(['code' => 100, 'msg' =>' 您无权修改排序顺序']);
        }
        $id = input('id');
        $param = [
            'pb_info_top'=>input('num'),
        ];
        $res = Db::table('up_user_banner')->where('pb_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
}